import { BasicColumn, FormSchema } from '@apps/antd-admin/src/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { setIsShelf, setIsShowShop } from '../goods/api';
import { render } from '@apps/antd-admin/src/hooks/render/renderUtils';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import { rules } from '@pkg/utils';

export const columns: BasicColumn[] = [
  {
    title: '保险名称',
    dataIndex: 'goodsName',
    width: 150,
  },
  {
    title: '保险价格(元)',
    dataIndex: 'goodsPrice',
    width: 120,
  },
  {
    title: '会员价格(元)',
    dataIndex: 'vipPrice',
    width: 120,
  },
  {
    title: '保险图片',
    dataIndex: 'coverImg',
    width: 100,
    customRender: render.renderImage,
  },
  {
    title: '销量',
    dataIndex: 'sales',
    width: 120,
  },
  {
    title: '库存',
    dataIndex: 'inventory',
    width: 120,
  },
  {
    title: '上下架',
    dataIndex: 'isShelf',
    width: 120,
    align: 'center',
    customRender: ({ record }: Recordable) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.isShelf,
        checkedValue: 1,
        unCheckedValue: 2,
        checkedChildren: '已上架',
        unCheckedChildren: '已下架',
        loading: record.pendingStatus,
        onChange(checked: number) {
          console.info(checked);
          record.pendingStatus = false;
          const { createMessage } = useMessage();
          setIsShelf(record.insuranceId, checked)
            .then(data => {
              record.isShelf = checked;
            })
            .catch(data => {
              console.info('catch', data);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '商城展示',
    dataIndex: 'isShowShop',
    width: 90,
    customRender: ({ record }: Recordable) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.isShowShop,
        checkedValue: 1,
        unCheckedValue: 2,
        checkedChildren: '展示',
        unCheckedChildren: '不展示',
        loading: record.pendingStatus,
        onChange(checked: number) {
          record.pendingStatus = false;
          const { createMessage } = useMessage();
          setIsShowShop(record.insuranceId, checked)
            .then(data => {
              record.isShowShop = checked;
            })
            .catch(data => {
              console.info('catch', data);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '创建日期',
    dataIndex: 'createTime',
    width: 140,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'goodsName',
    label: '延误险名称',
    component: 'Input',
    colProps: { span: 5 },
  },
  {
    field: 'isShelf',
    label: '是否上架',
    component: 'Select',
    colProps: { span: 5 },
    componentProps: {
      options: [
        { label: '上架', value: 1 },
        { label: '下架', value: 2 },
      ],
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'divider1',
    label: '延误险信息',
    component: 'Divider',
    helpMessage: '延误险信息',
  },
  {
    field: 'insuranceId',
    label: '延误险Id',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '延误险名称',
    field: 'goodsName',
    component: 'Input',
    required: true,
  },
  {
    label: '延误险价格',
    field: 'goodsPrice',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.price(true),
  },
  {
    label: '会员价格',
    field: 'vipPrice',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.price(true),
  },
  {
    label: '销量',
    field: 'sales',
    component: 'Input',
    required: true,
    dynamicRules: () => rules.digits(true),
  },
  {
    label: '库存 ',
    field: 'inventory',
    component: 'Input',
    required: true,
    dynamicRules: () => rules.digits(true),
  },
  {
    label: '排序',
    field: 'insuranceSort',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.digits(true),
  },
  {
    label: '封面图',
    field: 'coverImg',
    component: 'ImageUpload',
    required: false,
  },
  {
    label: '详情图',
    field: 'detailsImg',
    component: 'ImageUpload',
    required: false,
  },
  {
    field: 'divider2',
    label: '延误险设置',
    component: 'Divider',
    helpMessage: '延误险设置',
  },
  {
    label: '是否上架',
    field: 'isShelf',
    component: 'Select',
    required: true,
    defaultValue: 2,
    dynamicDisabled: true,
    componentProps: {
      options: [
        { label: '不上架', value: 2 },
        { label: '上架', value: 1 },
      ],
    },
  },
  {
    label: '预约控制(天)',
    field: 'maxTime',
    component: 'InputNumber',
    required: true,
  },
  {
    label: '商城展示',
    field: 'isShowShop',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        { label: '不展示', value: 2 },
        { label: '展示', value: 1 },
      ],
    },
  },
];
